package cn.smbms.controller;

import cn.smbms.pojo.Games;
import cn.smbms.service.GamesService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;

@Controller
@RequestMapping("/gamesManager")
public class GamesController {

    private static Logger logger=Logger.getLogger(GamesController.class);

    @Autowired
    private GamesService gamesService;

    @RequestMapping(value = "/list")
    public String getGames(Model model){
        Games games = new Games();
        List<Games> list = gamesService.getGames(games);
        //将信息保存到model中
        if (list != null) {
            model.addAttribute("list", list);

            return "list";
        }
        return "likeGameName";
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public String add(@ModelAttribute("gamesManager") Games games) {//
        logger.info("跳转到新增页面");
        return "add";
    }

    @RequestMapping(value = "/gamesManageraddsave", method = RequestMethod.POST)
    public String addSave(Games games) {
        try {
            if (gamesService.AddGames(games)){
                return "redirect:/gamesManager/list";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "add";
    }
}
